//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
**************************************************************************
*/

/**
* 工具栏插件类；
*/



define(function (require, exports, module) {
	
	FXREADER.WAP_RIBBON_TOOLBAR_PLUGIN_NAME = "Wap ToolBar Ribbon Plugin";
	
    FXREADER.CWapRibbonToolBarPlugin = function (pReaderApp) {
        var _pReaderApp = pReaderApp,
            _bInited = false,
            _Language = _pReaderApp.GetLanguage(),
             _self = this;

        var _RibbonDivID = _pReaderApp.GetTopPanelID()+"_ribbon";
        var _TopBarMainPageDivID = _RibbonDivID + "_topbar_mainpage";
        var _TopBarNavigationPanelDivID =  _RibbonDivID + "_topbar_navigation";
        var _MainPageRightToolBarDivID = _TopBarMainPageDivID+ "_righttools";
        var _NavigationExpandBtnDivID = _TopBarMainPageDivID + "navigationExpandBtn";
        var _NavigationHideBtnDivID = _TopBarNavigationPanelDivID + "navigationExpandBtn";
        
        //var _NavigationPanelRightToolBarDivID = _TopBarNavigationPanelDivID+ "_righttools";
        //获取Plugin名称;
        this.GetPluginName = function () {
            return FXREADER.WAP_RIBBON_TOOLBAR_PLUGIN_NAME;
        };

        //插件被注册的时候调用;
        this.PIOnRegister = function () {
            if (FXREADER.Config.ToolBar == null)
                return false;
            if (_bInited)
                return true;
            _self.CreateCmdAndCmdUI();
            var jqTopPanel = $("#" + _pReaderApp.GetTopPanelID());
            var ribbonPanelDiv = "<div id='"+_RibbonDivID+"' class='fwr_ribbon_top_panel_wap' />";
            jqTopPanel.append(ribbonPanelDiv);
            var jqRibbonDiv = $("#"+_RibbonDivID);
            
            var topBarMainPageDiv = "<div id='" + _TopBarMainPageDivID + "' class='fwr_ribbon_topbar1_wap'/>";
            jqRibbonDiv.append(topBarMainPageDiv);
            var jqTopBarMainPage = $("#"+_TopBarMainPageDivID);
            var navigationBtnDiv = "<div id='"+_NavigationExpandBtnDivID+"'class='fwr_toolbar_btn fwr_navigation_btn_frame_wap'><a class='fwr_navigation_btn_wap'/></div>";
            jqTopBarMainPage.append(navigationBtnDiv);
            var fileNameDivID = _TopBarMainPageDivID + "_filename";
            var fileNameDiv = "<span id='" + fileNameDivID + "' class='fwr_file_name_wap'>" + _pReaderApp.GetFileName() + "</span>" ;
            jqTopBarMainPage.append(fileNameDiv);
            var mainPageRightToolBarDiv = "<span class='fwr_ribbon_right_toolbar_wap' id='"+_MainPageRightToolBarDivID+"' />";
            jqTopBarMainPage.append(mainPageRightToolBarDiv);
            
            
            var topBarNavigationPanelDiv = "<div id='" + _TopBarNavigationPanelDivID + "' class='fwr_ribbon_topbar1_wap' style='display:none'/>";
            jqRibbonDiv.append(topBarNavigationPanelDiv);  
            var jqTopBarNavigationPanel = $("#"+_TopBarNavigationPanelDivID);
            var navigationBtnDiv = "<div id='"+_NavigationHideBtnDivID+"'class='fwr_toolbar_btn fwr_navigation_btn_frame_wap_right'><a class='fwr_navigation_btn_wap_right'/></div>";
            jqTopBarNavigationPanel.append(navigationBtnDiv);
            //var navigationPanelRightToolBarDiv = "<span class='fwr_ribbon_right_toolbar_wap' id='"+_NavigationPanelRightToolBarDivID+"' />";
            //jqTopBarNavigationPanel.append(navigationPanelRightToolBarDiv);
            
            jqTopPanel.show();
        };

        //模块初始化;
        this.PIInit = function () {
            if (FXREADER.Config.ToolBar == null)
                return false;
            if (_bInited)
                return true;
            $("#"+_NavigationExpandBtnDivID).bind("click",function(){
            	$("#fx_doc_qrcode_image").hide();
				if(FXREADER.WapNavigationPanelPluginName != null)
				{
					var wapNavigationPanelPlugin = _pReaderApp.GetPluginByName(FXREADER.WapNavigationPanelPluginName);
					if(wapNavigationPanelPlugin != null)
					{
						wapNavigationPanelPlugin.Show();
					}
				}
            	//$("#"+_TopBarMainPageDivID).hide();
            	//$("#"+_TopBarNavigationPanelDivID).show();
            });
            var jqRightToolBar = $("#"+_MainPageRightToolBarDivID);
            jqRightToolBar.append(_self.CreateToolBarHtml());
            _self.InitToolBarUI();
            $('.fwr_toolbar_btn').live('mousedown', function (event) {
                try {
                    if (event.button == 0 || event.button == 1) {
                        var cmdName = event.target.getAttribute("cmd");
                        _pReaderApp.ExecCommand(cmdName, null);
                    }
                }
                catch (ex) {
                }
            });
            _bInited = true;
        };

        this.CreateToolBarHtml = function () {
        	
        	var rightTools = FXREADER.Config.RibbonToolBar.right;
        	var rightToolsNum = rightTools.length;
        	var toolbarHtml = "";
        	for(var i = 0; i < rightToolsNum; i++)
        	{
        		 var btnName = rightTools[i];
                 var pCommandUI = _pReaderApp.GetCommandUI(btnName);
                 if (pCommandUI == null)
                     continue;
                 toolbarHtml += pCommandUI.CreateHtml();
        	}
            return toolbarHtml;
        };
        
        this.InitToolBarUI = function () {
        	var rightTools = FXREADER.Config.RibbonToolBar.right;
        	var rightToolsNum = rightTools.length;
        	for(var i = 0; i < rightToolsNum; i++)
        	{
        		 var btnName = rightTools[i];
                 var pCommandUI = _pReaderApp.GetCommandUI(btnName);
                 if (pCommandUI == null)
                     continue;
                 pCommandUI.Init();
        	}
        };

        this.CreateCmdAndCmdUI = function () {   
            _pReaderApp.AddCommandUI({
            	 UIName: "QRCode",
                 CommandName: null,
                 CreateHtmlContentFunc: function () {
                     return "<div class='fwr_toolbar_btn fwr_qrcode_frame_wap fwr_toolbar_btn_margin' id='fx_doc_qrcode' style='padding-left: 0px;padding-right: 0px;'><a class='fwr_qrcode_wap'/></div>";
                 },
                 Init: function () {
                 	var jqMainFrm = $("#" + _pReaderApp.GetMainView().GetMainFrameID());
                 	jqMainFrm.append("<div class='fwr_qrcode_image_div_wap fwr_hidden' id='fx_doc_qrcode_image'><img id='fwr_qrcode_image' alt='' src='' class='fwr_qrcode_image'></img></div>");
                 	var imgUrl = FXREADER.Config.Url.QrcodeUrl;
                 	$("#fwr_qrcode_image").attr("src", imgUrl);
					   $("#fx_doc_qrcode").bind("click",function(){
								$("#fx_doc_qrcode_image").toggle();
						});
					  
                 }
            });
        };
        //模块卸载;
        this.PIUnload = function () {
        };

        this.GetReaderApp = function () {
            return _pReaderApp;
        };
    };

    exports.Reader_CreatePlugin = function (pReaderApp) {
        var pToolBarPlugin = new FXREADER.CWapRibbonToolBarPlugin(pReaderApp);
        return pToolBarPlugin;
    };
});